var underscore = require('underscore')
var movie = require('../models/movie')
var Comment = require('../models/comment')
var User = require('../models/user')
var Category = require('../models/category')
var fs = require('fs')
var path = require('path')


//detail page
exports.detail = function(req,res){
	var id = req.params.id
	movie.findById(id,function(err,movie){
		Comment
			.find({movie: id})
			.populate('from','name')
			.populate('reply.from reply.to','name')
			.exec(function(err,comments){
				if(err){
					console.log(err)
				}
				console.log(comments)
				res.render('detail',{
					title: 'iMovie影片详情页',
					movie: movie,
					comments: comments
			})
		})
	})
	
}

//admin update movie
exports.update = function(req,res){
	var id=req.params.id
	movie.findById(id,function(err,movie){
		Category.find({},function(err,categories){
			res.render('admin',{
				title: 'iMovie影片详情页',
				movie: movie,
				categories: categories
			})
		})
	})

}

//admin post movie
exports.save = function(req,res){

	var id = req.body.movie._id
	
	var movieObj = req.body.movie

	var _movie

	if(id){
		movie.findById(id,function(err,movie){
			if(err){
				console.log(err)
			}
			//旧的分类id
			oldCategoryId = movie.category
			//新的分类id
			newCategoryId = movieObj.category

			_movie = underscore.extend(movie, movieObj);
			_movie.save(function(err,movie){
				if(err){
					console.log(err)
				}

				//不相等就是改变了 将原来的category中的movie去掉
				if(oldCategoryId != newCategoryId){
					//旧的分类
					Category.findById(oldCategoryId,function(err,category){
						if(err){
							console.log(err)
						}
						
						//迭代旧的分类下的电影的objectId
						category.movies.forEach(function(item,index){
							if(item.toString() == movie._id.toString()){
								category.movies.splice(index,1)
								return false
							}
						})

					
						category.save(function(err,category){
							if(err){
								console.log(err)
							}
							Category.findById(newCategoryId,function(err,category){
								category.movies.push(movie._id)
								category.save(function(err,category){
									res.redirect('/movie/' + movie._id)
								})
							})			
						})
					})
				}
			})
		})
	}else{

		_movie = new movie(movieObj)

		var categoryId =movieObj.category
		var categoryName = movieObj.categoryName

		_movie.save(function(err,movie){
			if(err){
				console.log(err)
			}

			if(categoryId){
				Category.findById(categoryId,function(err,category){
					category.movies.push(movie._id)
					category.save(function(err,category){
						res.redirect('/movie/' + movie._id)
					})
				})
			}else if(categoryName){
				var category = new Category({
					name: categoryName,
					movies: [movie._id]
				})

				category.save(function(err,category){
					movie.category = category._id
					movie.save(function(err,movie){
						res.redirect('/movie/' + movie._id)
					})
				})
			}
			
			
		})

	}
}

//list page
exports.list = function(req,res){
	movie.fetch(function(err,movies){
		res.render('list',{
			title: 'iMovie列表页',
			movies: movies
		})
	})
}

//list delete
exports.del = function(req,res){
	var id = req.query.id

	if(id){
		movie.remove({_id: id},function(err,movie){
			if(err){
				console.log(err)
			}else{
				res.json({success:1})
			}
		})
	}
}

//admin page
exports.new = function(req,res){
	Category.fetch(function(err,categories){
		res.render('admin',{
			title: 'iMovie后台管理',
			categories: categories,
			movie: {
				title: ' ',
				director: ' ',
				country: ' ',
				year: ' ',
				language: ' ',
				summary: ' ',
				poster: ' ',
				flash: ' '
			}
		})
	})
	
}